#include "handlercamera.h"

handlerCamera::handlerCamera(QObject *parent,ArClientBase *client) :
    QObject(parent),
    getImageCB(this,&handlerCamera::getImage),
    getCameraInfoCB(this,&handlerCamera::getCameraInfo),
    getCameraListCB(this,&handlerCamera::getCameraList)
{
    init = false;
    exitsCamera  = false;
    handledClient = client;

    if(handledClient->dataExists("getPictureCam1"))
      {

        handledClient->addHandler("getPictureCam1", &getImageCB);
        handledClient->request("getPictureCam1", 50);
        init = true;
      }
      else if(handledClient->dataExists("sendVideo"))
      {

        handledClient->addHandler("sendVideo", &getImageCB);
        handledClient->request("sendVideo", 50);
        init = true;
      }

    if(handledClient->dataExists("getCameraInfo")){
        name = "";
        handledClient->addHandler("getCameraInfo", &getCameraInfoCB);
        handledClient->requestOnce("getCameraInfo");

        exitsCamera = true;
    }else if(handledClient->dataExists("getCameraList")){
        handledClient->addHandler("getCameraList", &getCameraListCB);
        handledClient->requestOnce("getCameraList");

    }

}


void handlerCamera::getCameraInfo(ArNetPacket *pkt){
     int minPan = pkt->bufToByte2();
     int  maxPan = pkt->bufToByte2();
     int  minTilt = pkt->bufToByte2();
     int  maxTilt = pkt->bufToByte2();
     int  minZoom = pkt->bufToByte2();
     int  maxZoom = pkt->bufToByte2();
     int  haveZoom = pkt->bufToByte();

     emit signal_get_camera_values(minPan,maxPan,minTilt,maxTilt,minZoom,maxZoom,haveZoom);
}

void handlerCamera::getCameraList(ArNetPacket *pkt){

    int num = pkt->bufToByte2();


    if(num<1){
        exitsCamera = false;

        if(!init) emit signal_no_camera();
        return;

    }

    exitsCamera = true;
    name =  (char *)malloc(128);
    memset(name, 0, 128);
    pkt->bufToStr(name, 128);

    char command[145];
    sprintf(command,"getCameraInfo%s",name);
    handledClient->addHandler(command, &getCameraInfoCB);
    handledClient->requestOnce(command);

}


void handlerCamera::getImage(ArNetPacket *packet){

    unsigned int width;
      unsigned int height;
      static unsigned char jpeg[50000];
      int jpegSize;
      FILE *file;

      width = packet->bufToUByte2();
      height = packet->bufToUByte2();
      jpegSize = packet->getDataLength() - packet->getDataReadLength();
      if(jpegSize > 50000)
      {
        ArLog::log(ArLog::Normal, "Cannot save image, it's too big. (image is %d bytes, my buffer is 50000 bytes)", jpegSize);
        return;
      }
      packet->bufToData((char *)jpeg, jpegSize);





          emit signal_paint_image((uchar *)jpeg, jpegSize);


}


void handlerCamera::slot_updates_camera(int valuePan,int valueTilt,int valueZoom){
    if(!exitsCamera) return;
    ArNetPacket packet;
    char command[145];
    packet.empty();
    packet.byte2ToBuf(valuePan);
    packet.byte2ToBuf(valueTilt);
    packet.byte2ToBuf(valueZoom);
    sprintf(command,"setCameraAbs%s",name);
    handledClient->requestOnce(command, &packet);
}
